from product_upload.util.basic.mysql_util import db_get_one

class AmazonUsOrder:
    # 字段顺序严格与数据库表结构一致
    fields = [
        'id', 'order_id', 'order_status', 'order_type', 'order_total', 'quantity',
        'purchase_date', 'earliest_ship_date', 'latest_ship_date', 'name', 'address',
        'phone', 'skus', 'asins', 'basic_platforms', 'basic_skus', 'cost_price',
        'price_ratio', 'is_replacement', 'is_notify', 'opt_name', 'shop', 'screenshot',
        'remark', 'update_by', 'create_time', 'update_time', 'order_text'
    ]

    def __init__(self, **kwargs):
        # 按表字段顺序初始化属性
        self.id = kwargs.get('id', None)
        self.order_id = kwargs.get('order_id', None)
        self.order_status = kwargs.get('order_status', None)
        self.order_type = kwargs.get('order_type', None)
        self.order_total = kwargs.get('order_total', None)
        self.quantity = kwargs.get('quantity', None)
        self.purchase_date = kwargs.get('purchase_date', None)
        self.earliest_ship_date = kwargs.get('earliest_ship_date', None)
        self.latest_ship_date = kwargs.get('latest_ship_date', None)
        self.name = kwargs.get('name', None)
        self.address = kwargs.get('address', None)
        self.phone = kwargs.get('phone', None)
        self.skus = kwargs.get('skus', None)
        self.asins = kwargs.get('asins', None)
        self.basic_platforms = kwargs.get('basic_platforms', None)
        self.basic_skus = kwargs.get('basic_skus', None)
        self.cost_price = kwargs.get('cost_price', None)
        self.price_ratio = kwargs.get('price_ratio', None)
        self.is_replacement = kwargs.get('is_replacement', None)
        self.is_notify = kwargs.get('is_notify', None)
        self.opt_name = kwargs.get('opt_name', None)
        self.shop = kwargs.get('shop', None)
        self.screenshot = kwargs.get('screenshot', None)
        self.remark = kwargs.get('remark', None)
        self.update_by = kwargs.get('update_by', None)
        self.create_time = kwargs.get('create_time', None)
        self.update_time = kwargs.get('update_time', None)
        self.order_text = kwargs.get('order_text', None)

    @classmethod
    def from_db(cls, field_names, row) -> 'AmazonUsOrder':
        if len(field_names) != len(row):
            raise ValueError("bug!---db field len != query field len")
        data_dict = dict(zip(field_names, row))
        return cls(**data_dict)

    def __repr__(self):
        field_info = []
        for field in self.fields:
            value = getattr(self, field)
            field_info.append(f"{field}={repr(value)}")
        return f"AmazonUsOrder=[{', '.join(field_info)}]"


if __name__ == '__main__':
    # 保持相同的测试调用方式
    print(db_get_one("amazon_us_order", "id=3", AmazonUsOrder))